import socket

phone = socket.socket()

phone.bind(('127.0.0.1', 8888))

phone.listen(5)

# 4. 接收连接
while 1:
    print('start')
    conn, addr = phone.accept()  # 程序夯住
    print(conn,addr)
    while 1:
        try:
            from_client_data = conn.recv(1024)  #　至多接收1024个字节
            if from_client_data == b'q':
                break
            print(f'来自客户端{addr}消息{from_client_data.decode("utf-8")}')
            to_client = input('>>>')
            conn.send(to_client.encode('utf-8'))
        except ConnectionResetError:
            break
    conn.close()
phone.close()

